<template>
	<view>
		<view class="loginTitle">
			欢迎使用{{appName}}
		</view>
		
		<!-- <button class="loginBtn" v-if="!isOauthLogin" @tap="getUserInfo">微信授权登录</button> -->
		<button class="loginBtn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号快捷登录</button>
		<view class="agreementWrapper">
			<u-checkbox-group name="check" @change="checkChange()"><u-checkbox size="15" activeColor="#009CF9"></u-checkbox></u-checkbox-group>
			 我已阅读并同意
		     <text class="agreement" @click.stop="goAgreenment(13)">《服务协议》</text>
			 <text class="agreement" @click.stop="goAgreenment(12)">《隐私协议》</text>
		</view>
	</view>
</template>

<script>
	import {
		login,
		updatecustomer
	} from '@/api/login-api.js'
	import settings from '@/settings.js'
	export default {
		data() {
			return {
				checked :false,
				code : '',//微信临时登录凭证
				avatarUrl : '',
				nickName : '',
				isOauthLogin : false,
				systemInfo: {},
				appName : settings.appName
			}
		},
		computed: {
		  tenantId() {
			  return this.$store.state.user.tenantId;
		  } 
		},
		onLoad() {
		   this.getSystemInfo();
           this.weixinOauthByLogin();
		},
		methods: {
			
			/**
			 * 获取系统信息。
			 */
			async getSystemInfo() {
			    return new Promise((resolve, reject) => {
			        uni.getSystemInfo({
			            success: (res) => {
			                this.systemInfo = res;
			                resolve(res);
			            },
			            fail: (err) => {
			                uni.showToast({ title: '系统获取失败', icon: 'error' });
			                reject(err);
			            },
			        })
			    })
			},
			
		  //微信授权登录
          weixinOauthByLogin() {
			  uni.getProvider({
			  	service:'oauth',
			  	success: (res) => {
			  		console.log(res.provider);
			  		if(~res.provider.indexOf('weixin')){//判断如果是微信
			  			uni.login({
			  				provider: "weixin",
			  				success: (loginRes) => {
			  					this.code = loginRes.code;
			  				},
			  				fail: (error) => {
			  					console.log(error);
			  				}
			  			})
			  		}else {
			  			console.log('不是微信');
			  		}
			  		
			  	}
			  })	  
		  },
		  //获取用户信息
		  getUserInfo() {
			
			let that = this;
			uni.getUserProfile({
				desc:'获取你的昵称、头像、地区及性别',	
				success: (userRes) => {
					console.log('用户信息---', userRes.userInfo);
					let userInfo = userRes.userInfo;
					that.nickName = userInfo.nickName;
					that.avatarUrl = userInfo.avatarUrl;
					that.isOauthLogin = true;
					
				}
			})		  
			 
		  },
		  
		  //绑定手机号
		  getPhoneNumber(e) {
			  if(this.checked) {
				  let encryptedData = e.detail.encryptedData;
				  let iv = e.detail.iv;
				  var dataInfo = e.detail;
				  var that = this;
				  uni.checkSession({
				  	success(){
				  		//session_key未过期,并且在本声明周期一直存在
				  	},
				  	fail() {
				  		uni.login({
				  			provider: "weixin",
				  			success: (loginRes) => {
				  				that.code = loginRes.code;
				  				
				  			}
				  		})
				  	},
				  	complete(e) {
				  		// console.log(e);
				  		//获取openid
				  		login({
				  			code : that.code,
				  			encryptedData : encryptedData,
				  			iv : iv,
				  			tenantId : that.tenantId
				  		}).then(res=> {
				  			uni.showLoading({
				  				title:'登录中...'
				  			})
				  			   updatecustomer({
				  				   contactPhone : res.data.phoneNumber,
				  				   vxUrl : that.avatarUrl,
				  				   vxName : that.nickName,
				  				   openId : res.data.openId,
				  				   tenantId : that.tenantId,
				  				   memberSource : that.$store.state.user.memberSource,
								   companyNo : that.$store.state.user.inviterId,
								   phoneModel: that.systemInfo.model, // 手机型号
								   // qrCode: `pages/mine/mine?scene=${that.userInfo.memberNo}`,
				  			   }).then(res=> {
				  				   uni.hideLoading();
				  				   
				  				   //缓存登录状态
				  				   that.$store.commit('user/saveIsLogin', true);
				  				   uni.setStorageSync('isLogin', true);
				  				   
				  				   //缓存openid
				  				   that.$store.commit('user/saveOpenId', res.data.openId);
				  				   uni.setStorageSync('openid', res.data.openId);
				  				    //缓存用户信息
				  				   that.$store.commit('user/saveUserInfo', res.data);
				  				   uni.setStorageSync('userInfo', res.data);
				  				   //缓存认证状态
				  				   that.$store.commit('user/saveCensorStatus', res.data.censorStatus);
				  				   uni.setStorageSync('censorStatus', res.data.censorStatus);
				  				   
				  				   that.$u.toast("登录成功");
				  				   uni.$emit('loginRefresh');
				  				   uni.navigateBack({
				  				   	
				  				   })
				  				   
				  			   })
				  		})
				  	}
				  })
			  }else {
				  uni.showToast({
				  	title:"请阅读协议并勾选",
				      icon:"none"
				  })
			  }
			  
			  
		  },
		  //协议状态更新
		  checkChange() {
		  		this.checked = !this.checked;
		  },
		  //协议
		  goAgreenment(type) {
			  uni.navigateTo({
			  	url: '../guide/guide?type=' + type
			  })
		  }
		}
	}
</script>

<style lang="scss">
page{
	background-color: $bg-white-color;
}
	.loginTitle {
		margin: 50upx 0 0 50upx;
		font-size: 60upx;
		font-weight: bold;
	}
	.loginBtn {
		background-color: $main-color;
		margin: 200upx 50upx 0;
		height: 88upx;
		line-height: 88upx;
		border-radius: 16upx;
		color: $text-color-ff;
		font-size: 30upx;
		font-weight: bold;
		text-align: center;
	}
	
	.agreementWrapper {
		margin: 50upx;
		display: flex;
		color: $text-color-66;
		font-size: 24upx;
		.agreement {
			font-size: 24upx;
			color: $main-color;
		}
		
	}
</style>
